package traversal;


import java.util.*;

public class PostOrderTraversal {
    private static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val)
        {
            this.val = val;
        }
    }
    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {

            List<Integer> res = new ArrayList<>();
            if(root == null) return res ;
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while(!stack.isEmpty())
            {
                TreeNode node = stack.pop();
                res.add(node.val);
                if(node.left != null)
                {
                    stack.push(node.left);
                }
                if(node.right != null)
                {
                    stack.push(node.right);
                }
            }

            Collections.reverse(res);
            return res;
        }
    }
}
